package com.shop.shoporder.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.entity.order.*;
import com.shop.shoporder.mapper.order.CartMapper;
import com.shop.shoporder.mapper.order.ConfigMapper;
import com.shop.shoporder.mapper.order.UserAddressMapper;
import com.shop.shoporder.service.FinaceService;
import com.shop.shoporder.service.FreightService;
import com.shop.shoporder.service.SureOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("sureOrderService")
public class SureOrderServiceImpl implements SureOrderService {
    @Autowired
    UserAddressMapper userAddressMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    ConfigMapper configMapper;
    @Autowired
    FinaceService finaceService;
    @Autowired
    FreightService freightService;
    @Override
    public SureOrder getSureOrderList(Map<String, String> map, HttpServletRequest request) throws Exception {
        SureOrder sureOrder=new SureOrder();
        UserAddress userAddress=new UserAddress();
        Config config=new Config();
        List<CartList> cartList=new ArrayList<>();
        int userNoId = 0;
        String cartIds="";
        double deductionCoupon=0;//商品抵扣总购物券
        double deductionCashCoupon=0;//商品抵扣总代金券
        double balanceCoupon=0;
        double balanceCashCoupon=0;
        double freight=0;
        List<FreightData> listGuiGe=new ArrayList<>();
        List<String> cartIdsList=new ArrayList<>();
        if (map != null) {
            userNoId = InterfaceVer.VerNumber(map.get("userNoId"));
            cartIds = InterfaceVer.VerStr(map.get("cartIds"));
            if(!cartIds.equals("")){
                for (String str:cartIds.split(",")) {
                    int cartId= InterfaceVer.VerNumber(str);
                    if(cartId>0){
                        cartIdsList.add(str);
                    }
                }
            }
             userAddress=userAddressMapper.getAddressDefault(userNoId);

            config=configMapper.selectByPrimaryKey(1);
            Config config1=new Config();
            if(config!=null){
                config1.setFinacename2(config.getFinacename2());
                config1.setFinacename3(config.getFinacename3());
            }
            cartList=cartMapper.selectCartListSureOrder(userNoId,cartIdsList);
            //System.out.println("cartIdsList:"+cartIdsList+" cartList.size():"+cartList.size()+" userNoId:"+userNoId);
            for (CartList cartList1:cartList) {
                for (Carts carts:cartList1.getCartsList()) {
                    //System.out.println(cartList1.getCartsList().toString());
                    double a1=carts.getCoupon()==null?0:carts.getCoupon();
                    double a2=carts.getVouchers()==null?0:carts.getVouchers();
                    int productCount=carts.getProductCount()==null?0:carts.getProductCount();
                    deductionCoupon +=a1*productCount;
                    deductionCashCoupon +=a2*productCount;
                    System.out.println("a2:"+a2+" productCount:"+productCount);
                    //运费
                    Integer provinceId=0;
                    if(userAddress!=null){
                        int p=userAddress.getProvinceId()==null?0:userAddress.getProvinceId();
                        provinceId= InterfaceVer.VerNumber(p+"");
                    }
                    int guigeId=carts.getGuigeId()==null?0:carts.getGuigeId();
                    double weight=carts.getWeight()==null?0.00:carts.getWeight()/1000;
                    FreightData freightData=new FreightData();
                    freightData.setGuigeId(guigeId);
                    freightData.setNumber(productCount);
                    freightData.setWeight(weight);
                    freightData.setProvinceId(provinceId);
                    freightData.setWeightAll(weight*productCount);
                    listGuiGe.add(freightData);
                    //去除不要的字段
                    carts.setShopId(null);
                    carts.setGuigeId(null);
                    carts.setWeight(null);
                    carts.setCoupon(null);
                    carts.setVouchers(null);
                    carts.setShopName(null);
                }
            }
            //去除不要的字段
            if(userAddress!=null) {
                userAddress.setAreaId(null);
                userAddress.setProvinceId(null);
                userAddress.setCityId(null);
                userAddress.setIsdefault(null);
            }

            //System.out.println("deductionCoupon:"+deductionCoupon+" deductionCashCoupon:"+deductionCashCoupon+" userNoId:"+userNoId);
            JSONObject jsonObject=finaceService.getBalanceAllJ(userNoId);
            if(jsonObject!=null){
                balanceCoupon= InterfaceVer.VerDoubleNumber(jsonObject.get("balance2")==null?"":jsonObject.get("balance2").toString());
                balanceCashCoupon= InterfaceVer.VerDoubleNumber(jsonObject.get("balance3")==null?"":jsonObject.get("balance3").toString());
            }
            freight= freightService.getFreight(listGuiGe);
            if(deductionCoupon>balanceCoupon){
                deductionCoupon=balanceCoupon;
            }
            if(deductionCashCoupon>balanceCashCoupon){
                deductionCashCoupon=balanceCashCoupon;
            }
            //System.out.println("balanceCoupon:"+balanceCoupon+" balanceCashCoupon:"+balanceCashCoupon);
            sureOrder.setCartListList(cartList);
            sureOrder.setConfig(config1);
            sureOrder.setUserAddress(userAddress);
            sureOrder.setDeductionCoupon(deductionCoupon);
            sureOrder.setDeductionCashCoupon(deductionCashCoupon);
            sureOrder.setCoupon(balanceCoupon);
            sureOrder.setCashCoupon(balanceCashCoupon);
            sureOrder.setFreight(freight);
        }
        return sureOrder;
    }

    @Override
    public SureOrder getSureOrderList1(Map<String, String> map, HttpServletRequest request) throws Exception {
        SureOrder sureOrder=new SureOrder();
        UserAddress userAddress=new UserAddress();
        Config config=new Config();
        List<CartList> cartList=new ArrayList<>();
        int userNoId = 0;
        int addressId = 0;
        String cartIds="";
        double deductionCoupon=0;//商品抵扣总购物券
        double deductionCashCoupon=0;//商品抵扣总代金券
        double balanceCoupon=0;
        double balanceCashCoupon=0;
        double freight=0;
        double price=0;
        int type=0;
        List<FreightData> listGuiGe=new ArrayList<>();
        List<String> cartIdsList=new ArrayList<>();
        if (map != null) {
            userNoId = InterfaceVer.VerNumber(map.get("userNoId"));
            addressId = InterfaceVer.VerNumber(map.get("addressId"));
            //System.out.println("addressId:"+addressId);
            cartIds = InterfaceVer.VerStr(map.get("cartIds"));
            if(!cartIds.equals("")){
                for (String str:cartIds.split(",")) {
                    int cartId= InterfaceVer.VerNumber(str);
                    if(cartId>0){
                        cartIdsList.add(str);
                    }
                }
            }
            UserAddress userAddress1=new UserAddress();
            userAddress1.setUserNoId(userNoId);
            userAddress1.setId(addressId);
            userAddress=userAddressMapper.getAddressAll(userAddress1);

            config=configMapper.selectByPrimaryKey(1);
            Config config1=new Config();
            if(config!=null){
                config1.setFinacename2(config.getFinacename2());
                config1.setFinacename3(config.getFinacename3());
            }
            cartList=cartMapper.selectCartListSureOrder(userNoId,cartIdsList);
            //System.out.println("cartIdsList:"+cartIdsList+" cartList.size():"+cartList.size()+" userNoId:"+userNoId);
            for (CartList cartList1:cartList) {
                for (Carts carts:cartList1.getCartsList()) {
                    int zongid=carts.getZoneId()==null?0:carts.getZoneId();
                    if(zongid==13){
                        price +=carts.getPrice()==null?0:carts.getPrice();
                    }
                    //System.out.println(cartList1.getCartsList().toString());
                    double a1=carts.getCoupon()==null?0:carts.getCoupon();
                    double a2=carts.getVouchers()==null?0:carts.getVouchers();
                    int productCount=carts.getProductCount()==null?0:carts.getProductCount();
                    deductionCoupon +=a1*productCount;
                    deductionCashCoupon +=a2*productCount;
                    System.out.println("a2:"+a2+" productCount:"+productCount);
                    //运费
                    Integer provinceId=0;
                    if(userAddress!=null){
                        int p=userAddress.getProvinceId()==null?0:userAddress.getProvinceId();
                        provinceId= InterfaceVer.VerNumber(p+"");
                    }
                    int guigeId=carts.getGuigeId()==null?0:carts.getGuigeId();
                    double weight=carts.getWeight()==null?0.00:carts.getWeight()/1000;
                    FreightData freightData=new FreightData();
                    freightData.setGuigeId(guigeId);
                    freightData.setNumber(productCount);
                    freightData.setWeight(weight);
                    freightData.setProvinceId(provinceId);
                    freightData.setWeightAll(weight*productCount);
                    listGuiGe.add(freightData);
                    //去除不要的字段
                    carts.setShopId(null);
                    carts.setGuigeId(null);
                    carts.setWeight(null);
                    carts.setCoupon(null);
                    carts.setVouchers(null);
                    carts.setShopName(null);
                }
            }
            //去除不要的字段
            if(userAddress!=null) {
                userAddress.setAreaId(null);
                userAddress.setProvinceId(null);
                userAddress.setCityId(null);
                userAddress.setIsdefault(null);
            }

            //System.out.println("deductionCoupon:"+deductionCoupon+" deductionCashCoupon:"+deductionCashCoupon+" userNoId:"+userNoId);
            JSONObject jsonObject=finaceService.getBalanceAllJ(userNoId);
            if(jsonObject!=null){
                balanceCoupon= InterfaceVer.VerDoubleNumber(jsonObject.get("balance2")==null?"":jsonObject.get("balance2").toString());
                balanceCashCoupon= InterfaceVer.VerDoubleNumber(jsonObject.get("balance3")==null?"":jsonObject.get("balance3").toString());
            }
            //兑换专区算运费
//            if(price<3000){
//                type=2;
//            }else {
//                type=1;
//            }
            //
            freight= freightService.getFreight(listGuiGe);
            if(deductionCoupon>balanceCoupon){
                deductionCoupon=balanceCoupon;
            }
            if(deductionCashCoupon>balanceCashCoupon){
                deductionCashCoupon=balanceCashCoupon;
            }
            //System.out.println("balanceCoupon:"+balanceCoupon+" balanceCashCoupon:"+balanceCashCoupon);
            sureOrder.setCartListList(cartList);
            sureOrder.setConfig(config1);
            sureOrder.setUserAddress(userAddress);
            sureOrder.setDeductionCoupon(deductionCoupon);
            sureOrder.setDeductionCashCoupon(deductionCashCoupon);
            sureOrder.setCoupon(balanceCoupon);
            sureOrder.setCashCoupon(balanceCashCoupon);
            sureOrder.setFreight(freight);
        }
        return sureOrder;
    }
}
